package edu.zut.cs.software.cms.module.student.web.spring.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Optional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;

import edu.zut.cs.software.cms.base.web.spring.controller.GenericController;
import edu.zut.cs.software.cms.module.all.domain.All;
import edu.zut.cs.software.cms.module.student.domain.Student;
import edu.zut.cs.software.cms.module.student.domain.StudentArea;
import edu.zut.cs.software.cms.module.student.service.StudentAreaManager;
import edu.zut.cs.software.cms.module.student.service.StudentManager;

/**
 * 
 * @author 任雪燕
 *
 */
@Controller
@RequestMapping("/student")
@ResponseBody
public class StudentController extends GenericController<Student, Long, StudentManager>{
	
	StudentManager studentManager;
	
	StudentAreaManager studentAreaManager;

	@Autowired
	public void setStudentManager(StudentManager studentManager) {
		this.studentManager = studentManager;
		this.manager = this.studentManager;
	}
	
	@Autowired
	public void setStudentAreaManager(StudentAreaManager studentAreaManager) {
		this.studentAreaManager = studentAreaManager;
	}
	
	@RequestMapping(method = RequestMethod.GET, value = "/index.html")
	public String index() {
		String result = "/student/index";
		return result;
	}
	
	
	
//	@RequestMapping("/CreateOne")
//	@ResponseBody
//	public String CreateOne(String code,String name,String gender,String address,String tel,String email,String department,String major,String clazz) {
//		String flag="";
//		Student student = new Student();
//		StudentArea studentArea = new StudentArea();
//		if(studentManager.IfExist(code))//返回true
//		{
//			flag="true";
//			student.setCode(code);
//			student.setName(name);
//			student.setGender(gender);
//			student.setAddress(address);
//			student.setTel(tel);
//			student.setEmail(email);
//			
//			studentArea.setDepartment(department);
//			studentArea.setMajor(major);
//			studentArea.setClazz(clazz);
//			
//			studentArea.setParent(studentArea);
//			student.setStudentArea(studentArea);
//			
//			studentManager.save(student);
//			studentAreaManager.save(studentArea);
//		}
//		else {
//			flag="false";
//		}
//		HashMap<String,Object> res=new HashMap<String, Object>();//放入map集合K:V
//		res.put("flag", flag);//收容标记flag 前是字符串后是字符串对象定义的hashmap
////		res.put("token",token);
//		String res_json=JSON.toJSONString(res);//把一个对象转换为json串:把收容有flag标记和student对象转变成json数据
//		return res_json;//含有两块信息回应给前端
//	}
	
	
//	@RequestMapping("/DeleteById")
//	@ResponseBody
//	public String DeleteById(long id) {
//		studentManager.delete(id);
//		return "success!";
//	}
		
	
//	@RequestMapping("/ModifyByCode")
//	@ResponseBody
//	public String ModifyById(String code,String name,String gender,String address,String tel,String email) {
//		Student student = new Student();
//		
//		if (!studentManager.IfExist(code)) {
//			student.setCode(code);
//			student.setName(name);
//			student.setGender(gender);
//			student.setAddress(address);
//			student.setTel(tel);
//			student.setEmail(email);
//			
//			this.studentManager.save(student);
//			return "Modify success！";
//		}else {
//			return "Modify failure！";
//		}
//
//	}
//	
//	
//	@RequestMapping("/RetrieveById")
//	@ResponseBody
//	public Student RetrieveById(long id) {
//		Student student = new Student();
//		student = studentManager.findById(id);
//		return student;
//	}
	
	@ResponseBody
	@GetMapping(value = "findall", produces = "application/json;charset=utf-8")
	public String findAll() {
		List<Student> studentList = this.manager.getList();
		PageInfo pageInfo = new PageInfo(studentList);
		return JSON.toJSONString(pageInfo);
	}

	@GetMapping("/findbyid/{id}")
	public String fingById(@PathVariable("id") Long id) {
		Optional<Student> result = this.manager.findByStudentId(id);
		return JSON.toJSONString(result);
	}

	
	@GetMapping("/deleteById/{id}")
	public Boolean deleteById(@PathVariable("id") Long id) {
		Boolean result = this.manager.deleteById(id);
		return result;
	}
	
}
